﻿using System;
using Ue.CQRS.Samples.Domain.EventArgs;
using Ue.CQRS.Samples.Application.Query.Repositories;
using Ue.CQRS.Samples.Application.Query.DataContracts;

namespace Ue.CQRS.Samples.Application.Command.EventHandlers
{
    public class BookEventHandlers : Ue.CQRS.Samples.Domain.EventHandlers.IBookEventHandlers
    {
        public void BookCreateHandle(BookCreateEventArgs eventArgs)
        {
            var repository = RepositoryFactory.GetRepository<BookContract>();
            repository.AddOrUpdate(eventArgs.Id, new BookContract()
            {
                Id = eventArgs.Id,
                Author = eventArgs.Author,
                Description = eventArgs.Description,
                ISBN = eventArgs.ISBN,
                Name = eventArgs.Name,
                Page = eventArgs.Pager,
                Total = eventArgs.Total,
                Inventory = eventArgs.Total
            });
        }

        public void BookUpdateProfileHandle(BookUpdateProfileEventArgs eventArgs)
        {
            var repository = RepositoryFactory.GetRepository<BookContract>();
            var bookContract = repository[eventArgs.Id];
            bookContract.Name = eventArgs.Name;
            bookContract.Author = eventArgs.Author;
            bookContract.Description = eventArgs.Description;
            bookContract.ISBN = eventArgs.ISBN;
            bookContract.Page = eventArgs.Pager;

            repository.AddOrUpdate(eventArgs.Id, bookContract);
        }

        public void BookRemoveHandle(Guid id)
        {
            var repository = RepositoryFactory.GetRepository<BookContract>();
            repository.Remove(id);
        }
    }
}